const LikeModel = require('../../model/LikeModel');
const CollectModel = require('../../model/CollectionModel');
const ArticleModel = require('../../model/ArticleModel');
const { json } = require('express/lib/response');
// 确定，取消
const like = async (req, res) => {
    let { u_id, like_id } = req.body;
    console.log('文章id', like_id);
    console.log('用户id', u_id);
    try {
        //  不能重复
        let like_result = await LikeModel.where({ u_id, like_id }).findOne();
        console.log('不能重复不能重复--->', like_result);
        if (like_result) {
            let removeTem = await LikeModel.where({
                u_id, like_id
            }).remove();
            if (removeTem.deletedCount > 0) {
                await ArticleModel.updateOne({ _id: like_id }, {
                    $inc: {
                        u_like: -1
                    }
                });
            }
            res.json({
                status: 200,
                message: '取消点赞成功！'
            });
            console.log('存在----取消点赞成功！---', removeTem);
        } else {
            await LikeModel.create({
                u_id,
                like_id
            });
            await ArticleModel.updateOne({ _id: like_id }, {
                $inc: {
                    u_like: 1
                }
            });
            res.json({
                status: 200,
                message: '点赞成功！'
            });
            console.log('------不存在----点赞成功！---');
        }
    } catch (err) {
        console.log('点赞失败！', err);
        res.json({
            status: 500,
            message: '点赞失败！'
        })
    }
};
// 喜欢总数量
const likeNums = async (req, res) => {
    let { u_id } = req.body;
    try {
        let likeNumRe = await LikeModel.where({
            u_id
        }).find();
        //  当前所有文章总数
        let total = await LikeModel.where({ u_id }).find().countDocuments();
        console.log(likeNumRe, total);
        res.json({
            status: 200,
            likeNumRe,
            total: total,//总数
        })
    } catch (err) {
        console.log('获取数据失败！', err);
        res.json({
            status: 500,
            message: '获取数据失败！'
        })
    }
}
// 查询所有喜欢数据
const AllLike = async (req, res) => {
    let { u_id } = req.body;
    try {
        let tempArray = [];
        let All_likeNum = await LikeModel.where({
            u_id
        }).find();
        //  当前所有文章总数
        let likeTotal = await LikeModel.where({ u_id }).find().countDocuments();
        for (let key in All_likeNum) {
            tempArray.push(String(All_likeNum[key].like_id))
        }
        // console.log('数组-----',tempArray );
        // 6235d1bc7b7362fbf9966b00
        // 623552be45dc47545c2eaa3f
        // 6234a522897c96546e376521
        // 6235d1827b7362fbf9966afb
        // console.log('喜欢——id', All_likeNum);
        let all_like_data = await ArticleModel.find({ "_id": { "$in": tempArray } }).sort({ datetime: -1 })
        // let all_like_data = await ArticleModel.find({ "_id": { "$in": ["6235d1bc7b7362fbf9966b00", "623552be45dc47545c2eaa3f"] } })
        console.log(all_like_data);
        res.json({
            status: 200,
            all_like_data,
            total: likeTotal
        })
    } catch (err) {
        console.log('获取数据失败！', err);
        res.json({
            status: 500,
            message: '获取数据失败！'
        })
    }
}
// 确定，取消
const collect = async (req, res) => {
    let { u_id, collect_id } = req.body;
    try {
        //  不能重复
        let collect_result = await CollectModel.where({ u_id, collect_id }).findOne();
        console.log('不能重复不能重复--->', collect_result);
        if (collect_result) {
            let removeCollect = await CollectModel.where({
                u_id, collect_id
            }).remove();
            if (removeCollect.deletedCount > 0) {
                await ArticleModel.updateOne({ _id: collect_id }, {
                    $inc: {
                        u_collect: -1
                    }
                });
            }
            res.json({
                status: 200,
                message: '取消收藏成功！'
            });
            console.log('存在----取消点赞成功！---', removeCollect);
        } else {
            await CollectModel.create({
                u_id,
                collect_id
            });
            await ArticleModel.updateOne({ _id: collect_id }, {
                $inc: {
                    u_collect: 1
                }
            });
            res.json({
                status: 200,
                message: '收藏成功！'
            });
            console.log('------不存在----收藏成功！---');
        }
    } catch (err) {
        console.log('收藏失败！', err);
        res.json({
            status: 500,
            message: '收藏失败！'
        })
    }
}
// // 查询所有收藏数据
const AllCollect = async (req, res) => {
    let { u_id } = req.body;
    try {
        let tempArrayCo = [];
        let All_CollectNum = await CollectModel.where({
            u_id
        }).find();
        //  当前所有文章总数
        let collect_total = await CollectModel.where({ u_id }).find().countDocuments();
        for (let key in All_CollectNum) {
            tempArrayCo.push(String(All_CollectNum[key].collect_id))
        }
        console.log('数组-----', tempArrayCo);
        // 6235d1bc7b7362fbf9966b00
        // 623552be45dc47545c2eaa3f
        // 6234a522897c96546e376521
        // 6235d1827b7362fbf9966afb
        // console.log('喜欢——id', All_CollectNum);
        let all_Collect_data = await ArticleModel.find({ "_id": { "$in": tempArrayCo } }).sort({ datetime: -1 })
        // let all_Collect_data = await ArticleModel.find({ "_id": { "$in": ["6235d1bc7b7362fbf9966b00", "623552be45dc47545c2eaa3f"] } })
        console.log(all_Collect_data);
        res.json({
            status: 200,
            all_Collect_data,
            total: collect_total
        })
    } catch (err) {
        console.log('获取数据失败！', err);
        res.json({
            status: 500,
            message: '获取数据失败！'
        })
    }
}
module.exports = {
    like,
    likeNums,
    AllLike,
    collect,
    AllCollect
}